package cn.iocoder.yudao.module.wine.api;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wine.api.dto.PremiumRecordDTO;
import cn.iocoder.yudao.module.wine.controller.admin.premiumrecord.vo.PremiumRecordSaveReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorder.enums.SupplyOrderEnum;
import cn.iocoder.yudao.module.wine.dal.dataobject.premiumrecord.PremiumRecordDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.module.wine.service.premiumrecord.PremiumRecordService;
import cn.iocoder.yudao.module.wine.service.supplyorder.SupplyOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zyc
 * @createDate 2024/5/13 17:22
 */
@Service
@Validated
public class PremiumApiImpl implements PremiumApi{

    private static final Logger log = LoggerFactory.getLogger(PremiumApiImpl.class);
    @Resource
    private PremiumRecordService premiumRecordService;

    @Resource
    private SupplyOrderService supplyOrderService;

    @Override
    public PremiumRecordDTO getPremiumByOrder(Long userId, Long orderId) {
        PremiumRecordDO premiumByOrder = premiumRecordService.getPremiumByOrder(userId, orderId);
        return BeanUtils.toBean(premiumByOrder, PremiumRecordDTO.class);
    }

    @Override
    public PremiumRecordDTO getPremiumById(Long premiumId) {
        PremiumRecordDO premiumByOrder = premiumRecordService.getPremiumRecord(premiumId);
        return BeanUtils.toBean(premiumByOrder, PremiumRecordDTO.class);
    }

    @Override
    public void updateStatus(String tradeNo, Long curTenantId) {
        PremiumRecordDO premiumByOrder = premiumRecordService.getPremiumByTradeNo(tradeNo);
        //防止多次通知，写多记录
        if (premiumByOrder != null && premiumByOrder.getStatus() != 1) {
            //设置为已支付
            premiumByOrder.setStatus(1);
            premiumRecordService.updateStatus(premiumByOrder);

            //当前支付保费订单的用户ID
            Long curUserId = premiumByOrder.getUserId();

            //更新订单状态为已收款
            Long orderId = premiumByOrder.getOrderId();
            SupplyOrderDO supplyOrder = supplyOrderService.getSupplyOrder(orderId);
            Long buyUserId = supplyOrder.getBuyUserId();
            Long saleUserId = supplyOrder.getSaleUserId();

            PremiumRecordDO premiumBuyUserRecordDO = premiumRecordService.getPremiumByOrder(buyUserId, orderId);
            PremiumRecordDO premiumSaleUserRecordDO = premiumRecordService.getPremiumByOrder(saleUserId, orderId);

            if (premiumBuyUserRecordDO.getStatus() == 1 && Objects.equals(curUserId, buyUserId)) {
                log.info("买家{},支付担保费订单: {}", buyUserId, orderId);
                supplyOrderService.payUpdateSupplyOrder(supplyOrder, curTenantId, buyUserId); //买家付担保费
            } else if (premiumSaleUserRecordDO.getStatus() == 1 && Objects.equals(curUserId, saleUserId)) {
                log.info("卖家{},支付担保费订单: {}", saleUserId, orderId);
                supplyOrderService.payUpdateSupplyOrder(supplyOrder, curTenantId, saleUserId);//卖家付担保费
            }

            if (premiumBuyUserRecordDO.getStatus() == 1 && premiumSaleUserRecordDO.getStatus() == 1) {
                log.info("双方都付了担保费订单: {}", orderId);
                //双方都付了担保费
                //平台操作状态修改为未收买家货款，等待收买家货款
                supplyOrder.setOperateStatus(SupplyOrderEnum.OUTSTANDING_PAYMENT.getCode());
                supplyOrder.setStatus(SupplyOrderEnum.ACCOUNT_PAID.getCode());
                supplyOrderService.payUpdateSupplyOrder(supplyOrder, curTenantId, 0L);
            }
        }
    }

    @Override
    public void updateTradeNo(PremiumRecordDTO premiumRecordDTO) {
        PremiumRecordSaveReqVO bean = BeanUtils.toBean(premiumRecordDTO, PremiumRecordSaveReqVO.class);
        bean.setStatus(1);
        premiumRecordService.updatePremiumRecord(bean);
    }
}
